﻿using Hangfire;
using Hangfire.Storage.Monitoring;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using System.ComponentModel;

namespace HangfireExtensions
{
    [Route("hangfire/api")]
    [Tags("hangfire")]
    [EndpointGroupName("hangfire")]
    [Description("测试")]
    [ApiController()]
    [Authorize("admin")]
    public class HangFireController : ControllerBase
    {
        private readonly HangfireAPIService _hangfireAPIService;

        public HangFireController(HangfireAPIService hangfireAPIService)
        {
            _hangfireAPIService = hangfireAPIService;
        }



        [HttpPost("getFetchedJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetFetchedJobs([FromBody] PagePara<string> req)
        {

            return _hangfireAPIService.GetFetchedJobs(req);
        }


        [HttpPost("getScheduledJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetScheduledJobs(PageInput req)
        {
            return _hangfireAPIService.GetScheduledJobs(req);
        }




        [HttpPost("getEnqueuedJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetEnqueuedJobs(PagePara<string> req)
        {
            return _hangfireAPIService.GetEnqueuedJobs(req);
        }



        [HttpPost("getFailedJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetFailedJobs(PageInput req)
        {
            return _hangfireAPIService.GetFailedJobs(req);
        }


        [HttpPost("getProcessingJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetProcessingJobs(PageInput req)
        {
            return _hangfireAPIService.GetProcessingJobs(req);
        }



        [HttpPost("getSucceededJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetSucceededJobs(PageInput req)
        {
            return _hangfireAPIService.GetSucceededJobs(req);
        }


        [HttpPost("getDeletedJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetDeletedJobs(PageInput req)
        {
            return _hangfireAPIService.GetDeletedJobs(req);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("getAwaitingJobs")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetAwaitingJobs(PageInput req)
        {
            return _hangfireAPIService.GetAwaitingJobs(req);
        }
        /// <summary>
        /// 获取所有的周期性任务
        /// </summary>
        /// <returns></returns>
        [HttpPost("getRecurringJobs")]
        [EndpointSummary("获取所有的周期性任务")]
        public IResultDTO GetRecurringJobs()
        {
            return _hangfireAPIService.GetRecurringJobs();

        }
        
        [HttpGet("getQueues")]
        [EndpointSummary("获取任务")]
        public IResultDTO Queues()
        {
            return _hangfireAPIService.Queues();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet("getServers")]
        [EndpointSummary("获取任务")]
        public IResultDTO Servers()
        {
            return _hangfireAPIService.Servers();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet("getJobDetails")]
        [EndpointSummary("获取任务")]
        public IResultDTO JobDetails(string jobId)
        {
            return _hangfireAPIService.JobDetails(jobId);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet("getStatistics")]
        [EndpointSummary("获取任务")]
        public IResultDTO GetStatistics()
        {
            return _hangfireAPIService.GetStatistics();

        }



        /// <summary>
        /// 手动触发一个周期任务
        /// </summary>
        /// <param name="JobName"></param>
        /// <returns></returns>
        [HttpGet("trrigerJob")]
        [EndpointSummary("手动触发一个周期任务")]
        public IResultDTO<string> TrrigerJob(string JobName)
        {
            return _hangfireAPIService.TrrigerJob(JobName);
        }

        [HttpPost("trrigerJobs")]
        [EndpointSummary("手动触发一个周期任务")]
        public IResultDTO<string> TrrigerJobs([FromBody]string[] JobNames)
        {
            return _hangfireAPIService.TrrigerJobs(JobNames);
        }
        /// <summary>
        /// 删除一个周期任务
        /// </summary>
        /// <param name="JobName"></param>
        /// <returns></returns>
        [HttpGet("deleteRecurringJob")]
        [EndpointSummary("删除一个周期任务")]
        public IResultDTO DeleteRecurringJob(string JobName)
        {
            return _hangfireAPIService.DeleteRecurringJob(JobName);
        }
        /// <summary>
        /// 删除一个周期任务
        /// </summary>
        /// <param name="JobName"></param>
        /// <returns></returns>
        [HttpPost("deleteRecurringJobs")]
        [EndpointSummary("删除一个周期任务")]
        public IResultDTO DeleteRecurringJobs([FromBody]string[] JobNames)
        {
            return _hangfireAPIService.DeleteRecurringJobs(JobNames);
        }

        /// <summary>
        /// 删除一个延时任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet("deleteBackgroundJob")]
        [EndpointSummary("删除一个延时任务")]
        public IResultDTO DeleteBackgroundJob(string jobId)
        {
            return _hangfireAPIService.DeleteBackgroundJob(jobId);
        }


        /// <summary>
        /// 删除一个延时任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpPost("deleteBackgroundJobs")]
        [EndpointSummary("删除多个延时任务")]
        public IResultDTO DeleteBackgroundJobs(string[] jobIds)
        {
            return _hangfireAPIService.DeleteBackgroundJobs(jobIds);
        }
        /// <summary>
        /// 重新加一个延时任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet("reschedule")]
        [EndpointSummary("重新加一个延时任务")]
        public IResultDTO Reschedule(string jobId, DateTime enqueueAt)
        {
            return _hangfireAPIService.Reschedule(jobId, enqueueAt);
        }

        /// <summary>
        /// 重新加一个延时任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpPost("reschedules")]
        [EndpointSummary("重新加一个延时任务")]
        public IResultDTO Reschedules([FromQuery(Name = "at")] DateTime? enqueueAt, [FromBody] string[] jobIds)
        {
            return _hangfireAPIService.Reschedules(jobIds, enqueueAt ?? DateTime.Now);
        }



        [HttpPost("reschedule")]
        [EndpointSummary("重新加一个任务")]
        public IResultDTO ReRequeue([FromBody] string[] jobIds)
        {
            return _hangfireAPIService.ReRequeue(jobIds);
        }
    }
}
